/**
 * Copyright (C) 2011 Headvances Inc.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.

 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * This project aim to build a set of library/data to process 
 * the Vietnamese language and analyze the web data
 **/
package org.vsw.util;

import java.util.regex.Pattern;
/**
 * $Author: Tuan Nguyen$ 
 * $Revision$
 * $Date$
 * $LastChangedBy$
 * $LastChangedDate$
 * $URL$
 **/
public class ExceptionUtil {
  static public String getStackTrace(Throwable t, String filterExp) {
    Pattern filter = null ;
    if(filterExp != null) Pattern.compile(filterExp, Pattern.CASE_INSENSITIVE) ;
    StringBuilder b = new StringBuilder() ;
    b.append(t.getMessage()).append("\n") ;
    StackTraceElement[] elements = t.getStackTrace();
    for(StackTraceElement element : elements) {
      String eleString = element.toString() ;
      if(filter != null && !filter.matcher(eleString).matches()) continue ;
      b.append(element.toString()).append("\n") ;
    }
    return b.toString() ;
  }
  
  static public String getStackTrace(StackTraceElement[] elements, String filterExp) {
    Pattern filter = null ;
    if(filterExp != null) filter = Pattern.compile(filterExp, Pattern.CASE_INSENSITIVE) ;
    StringBuilder b = new StringBuilder() ;
    for(StackTraceElement element : elements) {
      String eleString = element.toString() ;
      if(filter != null && !filter.matcher(eleString).matches()) continue ;
      b.append(element.toString()).append("\n") ;
    }
    return b.toString() ;
  }
}